package com.yoomiito.app.ui.anyview.goodsinfo.bottom

import android.widget.LinearLayout
import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.BaseModel
import cn.droidlover.xdroidmvp.net.XApi
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.bean.GoodsDetail
import com.yoomiito.app.net.NetErrorCode
import com.yoomiito.app.net.YmApi
import com.yoomiito.app.showToast
import com.yoomiito.app.ui.order.sureorder.singleGoods.NewSureOrderActivity
import com.yoomiito.app.utils.*
import com.yoomiito.app.widget.dialog.GetFreeGoodsTipDialog
import kotlinx.android.synthetic.main.view_goodsinfo_bottombar_2.view.*

class FreeGoodsBottomBar(context: BaseActivity<*>) :BaseBottomBar(context){

    override fun getLayoutId(): Int = R.layout.view_goodsinfo_bottombar_2

    override fun getParentView(): LinearLayout = this

    override fun initView(goodsDetail: GoodsDetail) {
        receive.background = ColorUtils.gradientAndRadius("#FFFF035B","#FFFF246C",24f)
        receive.setOnClickListener {
            if (App.mAppConfig.isLogin){
                context.showLoading()
                pickSelect(goodsDetail)
            }else{
                LoginUtils.toOneKeyLogin(context)
            }
        }
    }

    private fun pickSelect(goodsDetail: GoodsDetail){
        YmApi.getYmService()
                .pickSelect()
                .compose(XApi.getApiTransformerNoData<BaseModel<*>>())
                .compose(XApi.getScheduler())
                .subscribe(object : ApiSubscriber<BaseModel<*>>(){
                    override fun onSuccess(t: BaseModel<*>?) {
                        context.dismissLoading()
                        NewSureOrderActivity.start(context,goodsDetail)
                    }

                    override fun onFail(error: ApiException?) {
                        context.dismissLoading()
                        when(error?.code){
                            NetErrorCode.SHOWDIALOG->{
                                val dialog = GetFreeGoodsTipDialog(context)
                                dialog.show()
                            }
                            NetErrorCode.SHOWTOAST->error?.msg?.showToast()
                            else -> error?.msg?.showToast()
                        }
                    }

                })
    }
}